package com.ukao.steward.ui.valetRunners.createOrder;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ukao.steward.R;
import com.ukao.steward.adapter.ClothingListAdapter;
import com.ukao.steward.base.BaseFragment;
import com.ukao.steward.bean.StringBean;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.consts.SaveParams;
import com.ukao.steward.dialog.UnTakeDialogFragment;
import com.ukao.steward.listener.DialogSubmitListener;
import com.ukao.steward.listener.MyTextWatcher;
import com.ukao.steward.retrofit.ApiCallback;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.L;
import com.ukao.steward.util.T;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 运单号
 */
public class TransNoFragment extends BaseFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER

    @BindView(R.id.back_btn)
    ImageView backBtn;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.title_layout)
    RelativeLayout titleLayout;
    @BindView(R.id.orderNo)
    TextView orderNo;
    @BindView(R.id.business)
    TextView business;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.sweep_btn)
    ImageView sweepBtn;

    @BindView(R.id.userRemark)
    EditText userRemark;
    @BindView(R.id.commit_btn)
    Button commitBtn;
    Unbinder unbinder;
    @BindView(R.id.trans_no)
    EditText transNo;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private String orderId;
    private String service;
    private String proPriceDesc;
    private ClothingListAdapter mClothingListAdapter;

    private List<StringBean> mClothinglist;
    private String strorderNo;


    /**
     * @param orderId      訂單
     * @param proPriceDesc 衣物明细
     * @param service      服务
     * @return
     */
    public static TransNoFragment newInstance(String orderId, String proPriceDesc, String service, String orderNo) {
        TransNoFragment fragment = new TransNoFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, orderId);
        args.putString(ARG_PARAM2, proPriceDesc);
        args.putString(Constant.ARG_PARAM3, service);
        args.putString(Constant.ARG_PARAM4, orderNo);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            orderId = getArguments().getString(ARG_PARAM1);
            proPriceDesc = getArguments().getString(ARG_PARAM2);
            service = getArguments().getString(Constant.ARG_PARAM3);
            strorderNo = getArguments().getString(Constant.ARG_PARAM4);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_trans_no, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initListener();
    }

    @Override
    protected void initView() {
        orderNo.setText(strorderNo);
        titleLayout.setBackgroundColor(getColors(R.color.colorPrimary));
        title.setText("录运单号");
        business.setText(service);
        Gson mGson = new Gson();
        if (!CheckUtils.isEmpty(proPriceDesc)) {  /*/*服务项目*/
            mClothinglist = mGson.fromJson(proPriceDesc,
                    new TypeToken<List<StringBean>>() {
                    }.getType());
        }
        if (CheckUtils.isEmpty(mClothinglist)) return;
        initLinearRecyclerView(recyclerView);
        mClothingListAdapter = new ClothingListAdapter(_mActivity, mClothinglist);
        recyclerView.setAdapter(mClothingListAdapter);
    }

    @Override
    protected void initListener() {
        super.initListener();
        transNo.addTextChangedListener(mTextWatcher);
    }

    private MyTextWatcher mTextWatcher = new MyTextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            super.onTextChanged(s, start, before, count);
            isILoginGo(s.toString().trim());
        }
    };

    private void isILoginGo(String s) {

        if (s.isEmpty()) {
            if (commitBtn.isEnabled()) {
                commitBtn.setAlpha(0.5f);
                commitBtn.setEnabled(false);
            }
        } else if (!commitBtn.isEnabled()) {
            commitBtn.setAlpha(1f);
            commitBtn.setEnabled(true);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.back_btn, R.id.commit_btn, R.id.sweep_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.commit_btn:

                addTransNo(orderId, getText(transNo));
                break;
            case R.id.sweep_btn:
                showTransNoDialog();
                break;
        }
    }


    private void showTransNoDialog() {
        UnTakeDialogFragment.createBuilder(getChildFragmentManager())
                .setmTitle("录用单号")
                .setListener(mDialogSubmitListener).show();


    }

    private DialogSubmitListener mDialogSubmitListener = new DialogSubmitListener() {
        @Override
        public void onSubmitClick(int parent, Object message) {
            transNo.setText((String) message);
        }
    };

    /**
     * 添加运单号
     *
     * @param orderId 订单号
     * @param transNo 运单号
     */
    public void addTransNo(String orderId, String transNo) {

        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("orderId", orderId);
        mHashMap.put("transNo", transNo);
        mHashMap.put("userRemark", getText(userRemark));
        mHashMap.put("accessToken", SaveParams.getToken());
        L.i("mHashMap=" + mHashMap.toString());
        showProgressDialog();
        addSubscription(apiStores().transNo(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode() == 200) {
                            finish();
                        }
                        T.show(model.getMsg());
                    }


                    @Override
                    public void onFinish() {

                        dismissProgressDialog();

                    }
                });

    }

}
